Core Data by Tutorials by By Pietro Rea & By Aaron Douglas & Matthew Morey

Core Data by Tutorials by By Pietro Rea & By Aaron Douglas & Matthew Morey

Author:By Pietro Rea & By Aaron Douglas & Matthew Morey
Language: eng
Format: epub
Publisher: Ray Wenderlich


Great! It seems like most things worked, but there are two problems. First, the console is warning you that the table view is laying out its cells before it's on screen, and the second is that the teams seem to be grouped by qualifying zone but the section headers are gone.

The console warning is happening because things are happening in a different order now. When the view controller was the data source of the table, and you were implementing the old fetched results controller delegate methods, then the table wasn't asking for any information until it was loaded and added to the screen. Now you're using a diffable data source, and the first change happens when you call performFetch() on the results controller, which in turn calls controller(_: didChangeContentWith:), which "adds" in all of the rows from the first fetch. You call performFetch() in viewDidLoad(), which happens before the view is added to the window. Phew!

To fix this, you need to perform the first fetch later on. Remove the do / catch statement from viewDidLoad(), since that's now happening too early in the lifecycle. Implement viewDidAppear(_:), which is called after the view is added to the window:

override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) UIView.performWithoutAnimation { do { try fetchedResultsController.performFetch() } catch let error as NSError { print("Fetching error: \(error), \(error.userInfo)") } } }

Build and run, and the console warning is gone. Now to fix the section headers.

Why have they disappeared? Earlier, when you removed the implementation of UITableViewDataSource, you also removed tableView(_:titleForHeaderInSection:). This method provided the strings to populate the section headers, and without those strings the headers disappeared.

There is no way to turn these headers back on with UITableViewDiffableDataSource so you'll take an alternate route. Find the section that implements UITableViewDelegate methods and implement these two:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let sectionInfo = fetchedResultsController.sections?[section] let titleLabel = UILabel() titleLabel.backgroundColor = .white titleLabel.text = sectionInfo?.name return titleLabel } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 20 }

Instead of just returning the title the populate the section headers, these two delegate methods create and return the UILabel to display along with the height of the section header.

Build and run to see if that brought back the missing headers:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.